/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package saa.dao.periodo;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import saa.dominio.periodo.OfertaDisciplina;

/**
 *
 * @author lobao
 */
@Stateless
public class OfertaDisciplinaDAO implements OfertaDisciplinaDAOInterface {

    @PersistenceContext(unitName = "SAAPU")
    private EntityManager em;

    @Override
    public boolean adicionar(OfertaDisciplina ofertaDisciplina) {
        try {
            em.persist(ofertaDisciplina);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean editar(OfertaDisciplina ofertaDisciplina) {
        try {
            em.merge(ofertaDisciplina);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public OfertaDisciplina buscar(long id) {
        try {
            return em.find(OfertaDisciplina.class, id);
        } catch (Exception e) {
            return null;
        }
    }

    public List<OfertaDisciplina> listarPorCurso(long cursoId) {
        try {
            return em
                    .createNamedQuery("OfertaDisciplina.disciplina.curso.id", OfertaDisciplina.class)
                    .setParameter("id", cursoId)
                    .getResultList();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean remover(long id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<OfertaDisciplina> listar() {
        try {
            return em
                    .createNamedQuery("OfertaDisciplinas", OfertaDisciplina.class)
                    .getResultList();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<OfertaDisciplina> listarPorDisciplina(long disciplinaId) {
        try {
            return em
                    .createNamedQuery("OfertaDisciplina.disciplina.id", OfertaDisciplina.class)
                    .setParameter("id", disciplinaId).getResultList();
        } catch (Exception e) {
            return null;
        }
    }

}
